package com.stone.config;

import com.stone.config.interceptor.AuthInterceptor;
import com.stone.config.interceptor.CommonInterceptor;
import com.stone.service.SysMenuService;
import com.stone.service.SysUserService;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import javax.annotation.Resource;

/**
 * @author stone
 * @date 2022/2/24 14:34
 */
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {

    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private ApplicationConfig applicationConfig;
    @Resource
    private SysUserService sysUserService;
    @Resource
    private SysMenuService sysMenuService;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        String[] excludes = new String[]{"/error", "/swagger-ui.html", "/swagger-resources/**", "/webjars/**"};
        //
        registry.addInterceptor(new AuthInterceptor(redisTemplate, applicationConfig, sysUserService, sysMenuService))
                .addPathPatterns("/**")
                .excludePathPatterns(excludes);
        registry.addInterceptor(new CommonInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns(excludes);
    }
}
